package com.gf.search.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gf.base.type.IndexType;
import com.gf.common.bean.Page;
import com.gf.exam.mapper.ExamMapper;
import com.gf.exam.model.Exam;
import com.gf.help.mapper.QuestionMapper;
import com.gf.help.model.Question;
import com.gf.search.dao.SearchDao;
import com.gf.search.service.SearchService;
import com.gf.user.mapper.UserMapper;
import com.gf.user.model.User;

@Service("searchService")
public class SearchServiceImpl implements SearchService {

	@Autowired
	private SearchDao searchDao;

	@Override
	public Page searchForUser(String keyword, int curpage, int pageSize) {
		Page page = new Page();
		page.setCurpage(curpage);
		page.setPageSize(pageSize);

		List<User> users = searchDao.search(keyword, IndexType.USER, page,
				UserMapper.getMapper());

		List<User> result = new ArrayList<User>();

		// 安全起见 将密码置为空
		for (int i = 0; i < users.size(); i++) {
			User user = users.get(i);
			user.setPassword("");
			result.add(user);
		}
		
		page.setResults(result);

		return page;
	}

	@Override
	public Page searchForQuestion(String keyword, int curpage, int pageSize) {
		Page page = new Page();
		page.setCurpage(curpage);
		page.setPageSize(pageSize);

		List<Question> questions = searchDao.search(keyword, IndexType.HELP,
				page, QuestionMapper.getMapper());

		page.setResults(questions);

		return page;
	}

	@Override
	public Page searchForExam(String keyword, int curpage, int pageSize) {
		Page page = new Page();
		page.setCurpage(curpage);
		page.setPageSize(pageSize);

		List<Exam> exams = searchDao.search(keyword, IndexType.EXAM, page,
				ExamMapper.getMapper());

		page.setResults(exams);

		return page;
	}

}
